﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.SessionState;
using AppShare.Core;
using AppShare.Data.Models;

namespace AppShare.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            App.InitializeAppDomainAndLogger();
            Log.WriteInfo("系统启动, 操作系统为{0}, 物理路径为{1}",
                Environment.OSVersion.VersionString,
                this.Server.MapPath(null));


            MSDNEntities.ConnectionString = WebConfigurationManager.ConnectionStrings["MSDNEntities"].ConnectionString;
            Log.WriteInfo("数据库：{0}", MSDNEntities.ConnectionString);


            this.Application["ship.htm"] = SharedUtil.GetHtmlFromResource("Templates.ship.htm");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Log.WriteInfo("来自{0}的用户{1}使用{2}浏览器请求{3}",
                 this.Request.UserHostAddress,
                 this.Request.LogonUserIdentity.Name,
                 this.Request.Browser.Type,
                 this.Request.RawUrl);

            // Route guide-page.html -> guides/page.aspx
            if (Request.Path.StartsWith("/guide-", StringComparison.InvariantCultureIgnoreCase)
                && Request.Path.EndsWith(".ashx", StringComparison.InvariantCultureIgnoreCase))
            {
                var routedPath = SharedUtil.RebuildUrl(Request.Path);
                if (routedPath == null)
                    Context.Response.StatusCode = 404;
                else
                    Context.RewritePath(routedPath);
            }
            else if (Request.Path.Equals("/guide.aspx", StringComparison.InvariantCultureIgnoreCase))
            {
                // Do not allow use or bot to access files under guides directly
                Context.Response.StatusCode = 404;
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Log.WriteException(this.Server.GetLastError());
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            Log.WriteInfo("系统关闭");
        }

        // Generate cache based on browser name and major (integer) version number
        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom == "Browser")
            {
                // IE6, IE7, IE8, Chrome5, Firefox3, Opera9, Safari2
                return context.Request.Browser.Type;
            }
            return String.Empty;
        }
    }
}